﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

using Weixin.Utils;

namespace Weixin.DAL
{
    public class CommonDal
    {
        /// <summary>
        /// 获取分页数据方法
        /// </summary>
        /// <param name="strTableName">表名,多表是请使用 tA a inner join tB b On a.AID = b.AID</param>
        /// <param name="strPrimaryKey">主键，可以带表头 a.AID</param>
        /// <param name="strSorter">排序字段和排序方向,为空时,默认按主键倒序排</param>
        /// <param name="iPageNumber">开始页码</param>
        /// <param name="iPageSize">页大小</param>
        /// <param name="strFields">读取字段</param>
        /// <param name="strCondition">Where条件</param>
        /// <param name="IsCount">是否获得总记录数</param>
        /// <param name="iTotalCount">查询到的总记录数</param>
        /// <returns></returns>
        public DataSet GetCurrentPageList(string strTableName, string strPrimaryKey, string strSorter,
            int iPageNumber, int iPageSize, string strFields, string strCondition,
            bool IsCount, out int iTotalCount)
        {
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Tables", SqlDbType.NVarChar,1000),
                new SqlParameter("@PK", SqlDbType.NVarChar,100),
                new SqlParameter("@Sort", SqlDbType.NVarChar,200),
                new SqlParameter("@PageNumber", SqlDbType.Int),  
                new SqlParameter("@PageSize", SqlDbType.Int), 
                new SqlParameter("@Fields", SqlDbType.NVarChar,4000),                                     
                new SqlParameter("@Filter", SqlDbType.NVarChar),              
                new SqlParameter("@Group", SqlDbType.NVarChar,1000),
                new SqlParameter("@isCount", SqlDbType.Bit),
                new SqlParameter("@TotalCounts", SqlDbType.Int)  
			};
            paras[0].Value = strTableName;
            paras[1].Value = strPrimaryKey;
            paras[2].Value = strSorter;
            paras[3].Value = iPageNumber;
            paras[4].Value = iPageSize;
            paras[5].Value = strFields;
            paras[6].Value = strCondition;
            paras[7].Value = null;
            paras[8].Value = IsCount;
            paras[9].Direction = ParameterDirection.Output;

            DataSet ds = new DataSet();
            ds = SqlHelper.ExecuteDataset(WebConfig.ChinaTax, CommandType.StoredProcedure, "p_PageListNew", paras);
            iTotalCount = Convert.ToInt32(paras[9].Value);
            return ds;
        }
    }
}
